﻿using AutoMapper;
using B.S.Traceable.Domain.Entitiy;
using B.S.Traceable.ErrorCode;
using B.S.Traceable.Infrasturcture.Interfaces;
using B.S.Traceable.Write.Api.Application.Command.Farming;
using MediatR;

namespace B.S.Traceable.Write.Api.Application.CommandHandler.Farming
{
    public class CreateFarmingHandler : IRequestHandler<CreateFarmingCommand, ApiResult<object>>
    {

        private readonly IBaseRepository<FarmingInformation> farRepo;
        private readonly IMapper mapper;

        public CreateFarmingHandler(IBaseRepository<FarmingInformation> farRepo, IMapper mapper)
        {
            this.farRepo = farRepo;
            this.mapper = mapper;
        }

        public Task<ApiResult<object>> Handle(CreateFarmingCommand request, CancellationToken cancellationToken)
        {
            ApiResult<object> result=new ApiResult<object>();
            var model=mapper.Map<FarmingInformation>(request);
            var s=farRepo.Add(model).Result;
            if (s > 0)
            {
                result.Code = ApiEnums.Success;
                result.Msg = "添加成功";
                result.Data = s;
            }
            else
            {
                result.Code = ApiEnums.Failed;
                result.Msg = "添加失败";
                result.Data = s;
            }
            return Task.FromResult(result);
        }
    }
}
